package queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-13 19:51
 **/
public class Test_ArrayBlockingQueue {


    /**
     * 阻塞队列、可以指定长度、线程安全
     */
    static BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(10);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            blockingQueue.put("a" + i);
        }
        //blockingQueue.put("aaa"); // 满了就会等待，程序阻塞
        //blockingQueue.add("aaa"); // 满了会报异常
        boolean flag = blockingQueue.offer("aaa");// 满了不会报异常，加不进去返回false
        blockingQueue.offer("aaa", 1, TimeUnit.SECONDS); // 可以尝试在一定时间内不断添加
        System.out.println(blockingQueue);
    }

}
